package ext_biz

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/text/gstr"
)

func ClearLog() {
	log_keep_day := g.Cfg().GetInt("ext_biz.log_keep_day")
	expireDate := gtime.Now().AddDate(0, 0, -log_keep_day)
	g.Log().Line(true).Debug("ClearLog")
	path := "./log"
	if gfile.Exists(path) {
		files, err := gfile.DirNames(path)
		if err != nil {
			g.Log().Line(true).Error(err)
		} else {
			if len(files) > 0 {
				for _, f := range files {

					fArr := gstr.Split(f, ".")
					if len(fArr) == 2 {
						name := fArr[0]
						dt := gtime.NewFromStr(name)
						if dt.Before(expireDate) {
							g.Log().Line(true).Debugf("remove file:%s", path+"/"+f)
							err = gfile.Remove(path + "/" + f)
							if err != nil {
								g.Log().Line(true).Error(err)
							}
						}
					} else {
						g.Log().Line(true).Warningf("illegal file name:%s", f)
					}
				}
			} else {
				g.Log().Line().Debug("no log file!")
			}
		}
	}
}
